package cn.sourcespro.spring.service.impl;

import cn.sourcespro.spring.crudparams.PageInfo;
import cn.sourcespro.spring.dao.RoleMapper;
import cn.sourcespro.spring.dao.UserMapper;
import cn.sourcespro.spring.entity.Role;
import cn.sourcespro.spring.entity.User;
import cn.sourcespro.spring.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户
 *
 * @author 张浩伟
 * @date 2018/07/25
 */
@Service
public class UserServiceImpl implements UserService, UserDetailsService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper mapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User add(User record) {

        int result = mapper.insert(record);
        if (result > 0) {
            return record;
        }
        return null;
    }

    @Override
    public User update(User record) {
        Integer id = record.getId();
        if (id == null) {
            return null;
        }
        User dbUser = mapper.selectByPrimaryKey(id);
        if (dbUser == null) {
            return null;
        }
        int result = mapper.updateByPrimaryKey(dbUser);
        if (result > 0){
            return dbUser;
        }
        return null;
    }

    @Override
    public User findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public User findByUuid(String uuid) {
        return mapper.findByUuid(uuid);
    }

    @Override
    public List<User> findAll(PageInfo pageInfo) {
        return mapper.selectAll(pageInfo);
    }

    @Override
    public int selectCount(PageInfo pageInfo) {
        return mapper.selectCount(pageInfo);
    }

    @Override
    public int delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(String uuid) {
        return mapper.deleteByUuid(uuid);
    }

    @Override
    public int delete(User record) {
        return delete(record.getId());
    }

    @Override
    public User findByUsername(String username) {
        return mapper.findByUsername(username);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录认证开始");
        User user = mapper.findByUsername(username);
        if (user == null) {
            logger.error("用户名[{}]不存在", username);
            throw new UsernameNotFoundException("用户名不存在");
        }
        logger.info("加载用户角色");
        List<Role> roleList = roleMapper.findByUserId(user.getId());
        List<String> roles = roleList.stream().map(Role::getName).collect(Collectors.toList());
        user.setRoles(roles);
        logger.info("用户信息：{}", user.toString());
        return user;
    }
}